<template>
  <div>
    <Card>
      <p slot="title">
        菜单管理
      </p>
      <div slot="extra">
        <Button class="icon-btn" type="success" @click="modalSetting.show = true">
          <span class="ml4">添加</span></Button>
      </div>
      <Table ref="tables" :data="tableData" :columns="columns" border disabled-hover/>
    </Card>

    <Modal v-model="modalSetting.show" width="668" :styles="{top: '20%'}" @on-visible-change="doCancel" :mask-closable="false">
      <p slot="header" style="color:#2d8cf0;">
        <Icon type="md-information-circle"></Icon>
        <span>{{formItem.id ? '编辑' : '新增'}}菜单</span>
      </p>
      <Form ref="myForm" :model="formItem" :label-width="80">
        <FormItem label="菜单名称" prop="name">
          <Input v-model="formItem.name" placeholder="请输入菜单名称"></Input>
        </FormItem>
        <FormItem label="父级菜单" prop="pid">
          <Select v-model="formItem.pid" filterable>
            <Option :value="0">顶级菜单</Option>
            <Option v-for="item in tableData" :value="item.id" :key="item.id">{{ item.showName }}</Option>
          </Select>
        </FormItem>
        <FormItem label="菜单Route" prop="route">
          <Input v-model="formItem.route" placeholder="请输入菜单Route">
          </Input>
        </FormItem>
        <FormItem label="菜单排序" prop="sort">
          <InputNumber :min="0" v-model="formItem.sort"></InputNumber>
          <Tag color="error" style="margin-left:5px">数字越小越靠前</Tag>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancel" style="margin-right: 8px">取消</Button>
        <Button type="primary" @click="handleAddSubmit" :loading="modalSetting.loading">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import Icons from '_c/icons'
import { getMenus, addMenu, editMenu, delMenu } from '@/api/system'
export default {
  name: 'menu_list',
  components: {
    Icons
  },
  data () {
    return {
      modalSetting: {
        show: false,
        loading: false,
        index: 0
      },
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      columns: [
        { title: '序号', key: 'id', width: 65, align: 'center' },
        { title: '权限名称', key: 'showName', align: 'left' },
        { title: '菜单Route', key: 'route', width: 200, align: 'center' },
        {
          title: '状态',
          key: 'hide',
          width: 100,
          align: 'center',
          render: (h, params) => {
            let _this = this
            let { id, status } = params.row
            return h('i-switch', {
              attrs: {
                size: 'large'
              },
              props: {
                'true-value': 1,
                'false-value': 0,
                value: status
              },
              on: {
                'on-change': (status) => {
                  editMenu(id, { status }).then(res => {
                    if (res.code === 1) {
                      _this.$Message.success(res.msg)
                    } else {
                      _this.$Message.error(res.msg)
                    }
                  })
                }
              }
            }, [
              h('span', {
                slot: 'open'
              }, '显示'),
              h('span', {
                slot: 'close'
              }, '隐藏')
            ])
          }
        },
        { title: '排序', key: 'sort', width: 100, align: 'center' },
        {
          title: '操作',
          key: 'action',
          width: 200,
          align: 'center',
          render: (h, params) => {
            let self = this
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click () {
                    let { name, pid, route, sort, id } = params.row
                    self.formItem = { name, pid, route, sort, id }
                    self.modalSetting.show = true
                  }
                }
              }, '编辑'),
              h('Poptip', {
                style: {
                  'textAlign': 'left'
                },
                props: {
                  confirm: true,
                  title: '确定删除？'
                },
                on: {
                  'on-ok': () => {
                    let { id } = params.row
                    delMenu(id).then(res => {
                      if (res.code === 1) {
                        this.$Message.success(res.msg)
                        this.getList()
                      } else {
                        this.$Message.error(res.msg)
                      }
                    })
                  }
                }
              }, [
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  }
                }, '删除')
              ])
            ])
          }
        }
      ],
      tableData: [],
      formItem: {
        name: '',
        pid: 0,
        route: '',
        sort: 0,
        id: 0
      },
      ruleValidate: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    doCancel (data) {
      if (!data) {
        this.formItem.id = 0
        this.$refs['myForm'].resetFields()
        this.modalSetting.loading = false
        this.modalSetting.index = 0
      }
    },
    cancel () {
      this.modalSetting.show = false
    },
    handleAddSubmit () {
      let self = this
      this.$refs['myForm'].validate((valid) => {
        if (valid) {
          self.modalSetting.loading = true
          if (this.formItem.id === 0) {
            addMenu(this.formItem).then(res => {
              if (res.code === 1) {
                self.$Message.success(res.msg)
                self.getList()
                self.cancel()
              } else {
                self.$Message.error(res.msg)
                self.cancel()
              }
            })
          } else {
            editMenu(this.formItem.id, this.formItem).then(res => {
              if (res.code === 1) {
                self.$Message.success(res.msg)
                self.getList()
                self.cancel()
              } else {
                self.$Message.error(res.msg)
                self.cancel()
              }
            })
          }
        }
      })
    },
    remove (index) {
      this.tableData.splice(index, 1)
      if (index === 0 && (this.page.current > 1)) {
        this.page.current = this.page.current - 1
        this.getList()
      } else {
        this.page.total = this.page.total - 1
      }
    },
    pageChange (page) {
      this.page.current = page
      this.getList()
    },
    getList () {
      getMenus().then(res => {
        if (res.code === 1) {
          this.tableData = res.data
        }
      })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>

<style>
.ivu-card-head p{
  height: 32px;
  line-height: 32px;
}
</style>
